﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using Utility;
namespace Service
{
    /// <summary>
    /// 功能：HW_Manger_Admin_User类数据访问
    /// 创建人：Wilson
    /// 创建时间：2013-1-29   
    /// </summary>
    public class HW_Manger_Admin_UserService1 : IDisposable
    {
        #region 公共
        private System.ComponentModel.Component component = new System.ComponentModel.Component();

        private bool disposed = false;

        ~HW_Manger_Admin_UserService1()
        {
            Dispose(false);
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {

                if (disposing)
                {

                    component.Dispose();
                }

                disposed = true;

            }
        }
        #endregion
				
        #region##通过主键ID得到HW_Manger_Admin_User
        /// <summary>
        /// 功能：通过ID得到HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_Manger_Admin_User GetById(string xx_username)
        {            
            HW_Manger_Admin_User obj = null;
            string sql = "select * from HW_Manger_Admin_User where XX_UserName = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",50)
            };
            
            SqlDataReader reader=SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql,sp);
            if (reader.Read())
            {
                obj = new HW_Manger_Admin_User();				
				obj.XX_UserName = (string)reader["XX_UserName"];
				obj.DistrictCode = (string)reader["DistrictCode"];
				obj.RoleId = (int)reader["RoleId"];
				obj.XX_Pass = (string)reader["XX_Pass"];
				obj.CNName = (string)reader["CNName"];
				obj.ErrorTimes = (int)reader["ErrorTimes"];
				obj.LastLoginTime = (DateTime)reader["LastLoginTime"];
				obj.Version = (int)reader["Version"];
				obj.CityId = (string)reader["CityId"];
				
            }
            return obj;
        }
        #endregion
		
		#region##通过主键ID删除HW_Manger_Admin_User
        /// <summary>
        /// 功能：通过主键ID删除HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(string xx_username)
		{
			int i = 0;
			string sql = "delete from HW_Manger_Admin_User where XX_UserName = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",50)
            };
            i= SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
			return i;
		}
        /// <summary>
        /// 功能：通过主键ID删除HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(SqlTransaction transaction,string xx_username)
		{
			int i = 0;
			string sql = "delete from HW_Manger_Admin_User where XX_UserName = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",50)
            };
            i= SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
			return i;
		}
		#endregion			
		
        #region##添加HW_Manger_Admin_User
        /// <summary>
        /// 功能：添加HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_Manger_Admin_User hw_manger_admin_user)
        {
            int result = 0;
            string sql = "insert into HW_Manger_Admin_User(XX_UserName,DistrictCode,RoleId,XX_Pass,CNName,ErrorTimes,LastLoginTime,Version,CityId) values(@XX_UserName,@DistrictCode,@RoleId,@XX_Pass,@CNName,@ErrorTimes,@LastLoginTime,@Version,@CityId)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@XX_UserName",hw_manger_admin_user.XX_UserName),
				new SqlParameter("@DistrictCode",hw_manger_admin_user.DistrictCode),
				new SqlParameter("@RoleId",hw_manger_admin_user.RoleId),
				new SqlParameter("@XX_Pass",hw_manger_admin_user.XX_Pass),
				new SqlParameter("@CNName",hw_manger_admin_user.CNName),
				new SqlParameter("@ErrorTimes",hw_manger_admin_user.ErrorTimes),
				new SqlParameter("@LastLoginTime",hw_manger_admin_user.LastLoginTime),
				new SqlParameter("@Version",hw_manger_admin_user.Version),
				new SqlParameter("@CityId",hw_manger_admin_user.CityId)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：添加HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(SqlTransaction transaction,HW_Manger_Admin_User hw_manger_admin_user)
        {
            int result = 0;
            string sql = "insert into HW_Manger_Admin_User(XX_UserName,DistrictCode,RoleId,XX_Pass,CNName,ErrorTimes,LastLoginTime,Version,CityId) values(@XX_UserName,@DistrictCode,@RoleId,@XX_Pass,@CNName,@ErrorTimes,@LastLoginTime,@Version,@CityId)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@XX_UserName",hw_manger_admin_user.XX_UserName),
				new SqlParameter("@DistrictCode",hw_manger_admin_user.DistrictCode),
				new SqlParameter("@RoleId",hw_manger_admin_user.RoleId),
				new SqlParameter("@XX_Pass",hw_manger_admin_user.XX_Pass),
				new SqlParameter("@CNName",hw_manger_admin_user.CNName),
				new SqlParameter("@ErrorTimes",hw_manger_admin_user.ErrorTimes),
				new SqlParameter("@LastLoginTime",hw_manger_admin_user.LastLoginTime),
				new SqlParameter("@Version",hw_manger_admin_user.Version),
				new SqlParameter("@CityId",hw_manger_admin_user.CityId)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##得到HW_Manger_Admin_User集合
		/// <summary>
        /// 功能：得到HW_Manger_Admin_User集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>       
        /// <returns></returns>
		public IList<HW_Manger_Admin_User> GetList()
		{
		    IList<HW_Manger_Admin_User> list = new List<HW_Manger_Admin_User>();
            HW_Manger_Admin_User obj = null;				
			string sql = "select * from HW_Manger_Admin_User";			
			SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql);
			while(reader.Read())
			{
				obj = new HW_Manger_Admin_User();	
				obj.XX_UserName = (string)reader["XX_UserName"];
				obj.DistrictCode = (string)reader["DistrictCode"];
				obj.RoleId = (int)reader["RoleId"];
				obj.XX_Pass = (string)reader["XX_Pass"];
				obj.CNName = (string)reader["CNName"];
				obj.ErrorTimes = (int)reader["ErrorTimes"];
				obj.LastLoginTime = (DateTime)reader["LastLoginTime"];
				obj.Version = (int)reader["Version"];
				obj.CityId = (string)reader["CityId"];
				list.Add(obj);				
			}
            return list;
		}
		#endregion
		
		#region##修改HW_Manger_Admin_User
        /// <summary>
        /// 功能：修改HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_Manger_Admin_User(HW_Manger_Admin_User hw_manger_admin_user)
        {
            int result = 0;
            string sql = "update HW_Manger_Admin_User set XX_UserName = @XX_UserName,DistrictCode = @DistrictCode,RoleId = @RoleId,XX_Pass = @XX_Pass,CNName = @CNName,ErrorTimes = @ErrorTimes,LastLoginTime = @LastLoginTime,Version = @Version,CityId = @CityId where XX_UserName = @XX_UserName";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@XX_UserName",hw_manger_admin_user.XX_UserName),
				new SqlParameter("@DistrictCode",hw_manger_admin_user.DistrictCode),
				new SqlParameter("@RoleId",hw_manger_admin_user.RoleId),
				new SqlParameter("@XX_Pass",hw_manger_admin_user.XX_Pass),
				new SqlParameter("@CNName",hw_manger_admin_user.CNName),
				new SqlParameter("@ErrorTimes",hw_manger_admin_user.ErrorTimes),
				new SqlParameter("@LastLoginTime",hw_manger_admin_user.LastLoginTime),
				new SqlParameter("@Version",hw_manger_admin_user.Version),
				new SqlParameter("@CityId",hw_manger_admin_user.CityId)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：修改HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_Manger_Admin_User(SqlTransaction transaction,HW_Manger_Admin_User hw_manger_admin_user)
        {
            int result = 0;
            string sql = "update HW_Manger_Admin_User set XX_UserName = @XX_UserName,DistrictCode = @DistrictCode,RoleId = @RoleId,XX_Pass = @XX_Pass,CNName = @CNName,ErrorTimes = @ErrorTimes,LastLoginTime = @LastLoginTime,Version = @Version,CityId = @CityId where XX_UserName = @XX_UserName";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@XX_UserName",hw_manger_admin_user.XX_UserName),
				new SqlParameter("@DistrictCode",hw_manger_admin_user.DistrictCode),
				new SqlParameter("@RoleId",hw_manger_admin_user.RoleId),
				new SqlParameter("@XX_Pass",hw_manger_admin_user.XX_Pass),
				new SqlParameter("@CNName",hw_manger_admin_user.CNName),
				new SqlParameter("@ErrorTimes",hw_manger_admin_user.ErrorTimes),
				new SqlParameter("@LastLoginTime",hw_manger_admin_user.LastLoginTime),
				new SqlParameter("@Version",hw_manger_admin_user.Version),
				new SqlParameter("@CityId",hw_manger_admin_user.CityId)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##统计HW_Manger_Admin_User
		/// <summary>
        /// 修改HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			int i = 0;
            string sql = "select count(*) from HW_Manger_Admin_User";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
		}
		/// <summary>
        /// 修改HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            int i=0;
            string sql="select count(XX_UserName) from HW_Manger_Admin_User";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
        }
		#endregion
        #region##自定义HW_Manger_Admin_User
        public IList<HW_Manger_Admin_User> GetAdminByDisLevel(int level)
        {
            string sql=@"select * from HW_Manger_Admin_User a inner join HW_District b 
                        on a.DistrictCode=b.DistrictCode inner join HW_Admin_Detail c on 
                         a.XX_UserName=c.XX_UserName where b.Level=@level";
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@level",level),
            };
            IList<HW_Manger_Admin_User> list = new List<HW_Manger_Admin_User>();
            HW_Manger_Admin_User obj = null;
		    HW_Admin_Detail detail=null;
            HW_District district = null;
			//string sql = "select * from HW_Manger_Admin_User";			
			SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql,sp);
			while(reader.Read())
			{
				obj = new HW_Manger_Admin_User();	
				obj.XX_UserName = (string)reader["XX_UserName"];
				obj.DistrictCode = (string)reader["DistrictCode"];
				obj.RoleId = (int)reader["RoleId"];
				obj.XX_Pass = (string)reader["XX_Pass"];
				obj.CNName = (string)reader["CNName"];
				obj.ErrorTimes = (int)reader["ErrorTimes"];
				obj.LastLoginTime = (DateTime?)reader["LastLoginTime"];
				obj.Version = (int)reader["Version"];
				obj.CityId = (string)reader["CityId"];

                detail.XX_UserName=reader["XX_UserName"].ToString();
                detail.Address=reader["Address"].ToString();
                detail.Email=reader["Email"].ToString();
                detail.Fax=reader["Fax"].ToString();
                detail.MobilePhone=reader["MobilePhone"].ToString();
                detail.OfficePhone=reader["OfficePhone"].ToString();
                detail.WorkUnit=reader["WorkUnit"].ToString();
                detail.ZipCode=reader["ZipCode"].ToString();

                district = new HW_District();
                district.DistrictCode = reader["DistrictCode"].ToString();
                district.Name = reader["Name"].ToString();
                district.ParentDistrictCode = reader["ParentDistrictCode"].ToString();
                district.SchoolNumber = Convert.ToInt32(reader["SchoolNumber"]);
                district.Sort = Convert.ToInt32(reader["Sort"]);
                obj.hW_Admin_Detail=detail;
                obj.hW_District = district;
				list.Add(obj);				
			}
            return list;
        }
         public IList<HW_Manger_Admin_User> GetAdminByDisLevel(string pdistrictCode,int level)
        {
            string sql=@"select * from HW_Manger_Admin_User a inner join HW_District b 
                        on a.DistrictCode=b.DistrictCode left join HW_Admin_Detail c on 
                         a.XX_UserName=c.XX_UserName where b.Level=@level and b.ParentDistrictCode=@pdistrictCode";
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@level",level),
                new SqlParameter("@pdistrictCode",pdistrictCode),
            };
            IList<HW_Manger_Admin_User> list = new List<HW_Manger_Admin_User>();
            HW_Manger_Admin_User obj = null;
		    HW_Admin_Detail detail=null;
            HW_District district = null;
			//string sql = "select * from HW_Manger_Admin_User";			
			SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql,sp);
			while(reader.Read())
			{
				obj = new HW_Manger_Admin_User();	
				obj.XX_UserName = (string)reader["XX_UserName"];
				obj.DistrictCode = (string)reader["DistrictCode"];
				obj.RoleId = (int)reader["RoleId"];
				obj.XX_Pass = (string)reader["XX_Pass"];
				obj.CNName = (string)reader["CNName"];
				obj.ErrorTimes = (int)reader["ErrorTimes"];
				obj.LastLoginTime = reader["LastLoginTime"] as DateTime?;
				obj.Version = (int)reader["Version"];
				obj.CityId = (string)reader["CityId"];

                detail.XX_UserName=reader["XX_UserName"].ToString();
                detail.Address=reader["Address"].ToString();
                detail.Email=reader["Email"].ToString();
                detail.Fax=reader["Fax"].ToString();
                detail.MobilePhone=reader["MobilePhone"].ToString();
                detail.OfficePhone=reader["OfficePhone"].ToString();
                detail.WorkUnit=reader["WorkUnit"].ToString();
                detail.ZipCode=reader["ZipCode"].ToString();

                district = new HW_District();
                district.DistrictCode = reader["DistrictCode"].ToString();
                district.Name = reader["Name"].ToString();
                district.ParentDistrictCode = reader["ParentDistrictCode"].ToString();
                district.SchoolNumber = Convert.ToInt32(reader["SchoolNumber"]);
                district.Sort = Convert.ToInt32(reader["Sort"]);
                obj.hW_Admin_Detail=detail;
                obj.hW_District = district;
				list.Add(obj);				
			}
            return list;
        }
         public IList<HW_Manger_Admin_User> Mapper(SqlDataReader reader, IList<HW_Manger_Admin_User> list)
         {
             HW_Manger_Admin_User obj = null;
             HW_Admin_Detail detail = null;
             HW_District district = null;
             while (reader.Read())
             {
                 obj = new HW_Manger_Admin_User();
                 obj.XX_UserName = (string)reader["XX_UserName"];
                 obj.DistrictCode = (string)reader["DistrictCode"];
                 obj.RoleId = (int)reader["RoleId"];
                 obj.XX_Pass = (string)reader["XX_Pass"];
                 obj.CNName = (string)reader["CNName"];
                 obj.ErrorTimes = (int)reader["ErrorTimes"];
                 obj.LastLoginTime = (DateTime?)reader["LastLoginTime"];
                 obj.Version = (int)reader["Version"];
                 obj.CityId = (string)reader["CityId"];

                 detail.XX_UserName = reader["XX_UserName"].ToString();
                 detail.Address = reader["Address"].ToString();
                 detail.Email = reader["Email"].ToString();
                 detail.Fax = reader["Fax"].ToString();
                 detail.MobilePhone = reader["MobilePhone"].ToString();
                 detail.OfficePhone = reader["OfficePhone"].ToString();
                 detail.WorkUnit = reader["WorkUnit"].ToString();
                 detail.ZipCode = reader["ZipCode"].ToString();

                 district = new HW_District();
                 district.DistrictCode = reader["DistrictCode"].ToString();
                 district.Name = reader["Name"].ToString();
                 district.ParentDistrictCode = reader["ParentDistrictCode"].ToString();
                 district.SchoolNumber = Convert.ToInt32(reader["SchoolNumber"]);
                 district.Sort = Convert.ToInt32(reader["Sort"]);
                 obj.hW_Admin_Detail = detail;
                 obj.hW_District = district;
                 list.Add(obj);
             }
             return list;
         }
         public HW_Manger_Admin_User Mapper(SqlDataReader reader, HW_Manger_Admin_User obj)
         {
             HW_Admin_Detail detail = null;
             HW_District district = null;
             if (reader.Read())
             {
                 //obj = new HW_Manger_Admin_User();
                 obj.XX_UserName = (string)reader["XX_UserName"];
                 obj.DistrictCode = (string)reader["DistrictCode"];
                 obj.RoleId = (int)reader["RoleId"];
                 obj.XX_Pass = (string)reader["XX_Pass"];
                 obj.CNName = (string)reader["CNName"];
                 obj.ErrorTimes = (int)reader["ErrorTimes"];
                 obj.LastLoginTime = (DateTime?)reader["LastLoginTime"];
                 obj.Version = (int)reader["Version"];
                 obj.CityId = (string)reader["CityId"];

                 detail.XX_UserName = reader["XX_UserName"].ToString();
                 detail.Address = reader["Address"].ToString();
                 detail.Email = reader["Email"].ToString();
                 detail.Fax = reader["Fax"].ToString();
                 detail.MobilePhone = reader["MobilePhone"].ToString();
                 detail.OfficePhone = reader["OfficePhone"].ToString();
                 detail.WorkUnit = reader["WorkUnit"].ToString();
                 detail.ZipCode = reader["ZipCode"].ToString();

                 district = new HW_District();
                 district.DistrictCode = reader["DistrictCode"].ToString();
                 district.Name = reader["Name"].ToString();
                 district.ParentDistrictCode = reader["ParentDistrictCode"].ToString();
                 district.SchoolNumber = Convert.ToInt32(reader["SchoolNumber"]);
                 district.Sort = Convert.ToInt32(reader["Sort"]);
                 obj.hW_Admin_Detail = detail;
                 obj.hW_District = district;
             }
             return obj;
         }
        #endregion
                
    }
}
